<?php
/**
 * 订单管理
 * @author Administrator
 */
class order extends admin{
   public function __construct(){
      parent::__construct();
   }

   //订单列表
   public function index(){
      $start = isset($_GET['time'][0])&&!empty($_GET['time'][0])?strtotime($_GET['time'][0]):0;
      $end = isset($_GET['time'][1])&&!empty($_GET['time'][1])?strtotime($_GET['time'][1]):NOW;
      $where ='where pay_time between '.$start.' and '.$end;
      $school_id=(int)$_SESSION['client']['jx_id'];
      $where.= ' and school_id='.$school_id;
      if(isset($_GET['bm_id'])&&(int)$_GET['bm_id']>0) $where.=' and bm_id='.(int)$_GET['bm_id'];
      if(isset($_GET['order_no'])&&$_GET['order_no']!='') $where.=' and order_no="'.trim($_GET['order_no']).'"';
      if(isset($_GET['pay_type'])&&is_numeric($_GET['pay_type'])) $where.=' and pay_type='.(int)$_GET['pay_type'];
      if(isset($_GET['status'])&&is_numeric($_GET['status'])) $where.=' and status='.(int)$_GET['status'];
      $total=db()->getOne('select count(*) from x_order '.$where);
      $pagesize = C('PAGE_SIZE');
      if (isset($_GET['p']) && (int) $_GET['p'] > 0) $p = (int) $_GET['p'];
      else $p = 1;
      $limit=' limit '.($p-1)*$pagesize.','.$pagesize;
      $sql = 'select * from x_order '.$where.' order by id DESC '.$limit;
      $list=db()->getAll($sql);
      $page = array('total' => $total, 'pagesize' => $pagesize, 'curpage' => $p);
      $this->ajaxReturn(array('status'=>1,'data'=>$list,'page'=>$page));
   }

   public function detail(){
      $id=(int)$_GET['id'];
      $res=db()->getRow('select * from x_order where id='.$id);
      $tpl=$this->Template();
      $tpl->assign('vo',$res);
      $tpl->display();
   }

   public function baoming(){
        $start = isset($_GET['price_min'])&&!empty($_GET['price_min'])?strtotime($_GET['price_min']):0;
        $end = isset($_GET['price_max'])&&!empty($_GET['price_max'])?strtotime($_GET['price_max']):NOW;
        $where ='where create_time between '.$start.' and '.$end;

        if(isset($_GET['bm_id'])&&$_GET['bm_id']!='') $where.=' and id="'.(int)($_GET['bm_id']).'"';

        if(isset($_GET['mobile'])&&$_GET['mobile']!='') $where.=' and mobile="'.trim($_GET['mobile']).'"';
        if(isset($_GET['pay_type'])&&(int)$_GET['pay_type']>0) $where.=' and pay_type='.(int)$_GET['pay_type'];
        if(isset($_GET['status'])&&is_numeric($_GET['status'])) $where.=' and status='.(int)$_GET['status'];
        if(isset($_GET['taocan_id'])&&(int)$_GET['taocan_id']>0) $where.=' and taocan_id='.(int)$_GET['taocan_id'];

        $count=db()->getOne('select count(*) from x_baoming '.$where);
        $pagesize=C('PAGE_SIZE');
        if(isset($_GET['page'])&&(int)$_GET['page']>0) $p=(int)$_GET['page'];
        else $p=1;
        $max_p = ceil($count/$pagesize);
        if($p>=$max_p) $p = $max_p;
        $limit=' limit '.($p-1)*$pagesize.','.$pagesize;

        $sql = 'select id,real_name,mobile,school_name,taocan_name,taocan_price,yhq_price,has_pay,status,create_time from x_baoming '.$where.' order by id DESC '.$limit;
        $list=db()->getAll($sql);
        $tpl=$this->Template();
        $tpl->assign('list',$list);
        $page=pages($pagesize,$count);
        $tpl->assign('page',$page);
        $tpl->display();
   }

   //线下支付——添加
   public function add(){
        if (IS_AJAX){
            $bm_id=(int)$_GET['bm_id'];
            $uid = db()->getOne('select uid from x_baoming where id='.$bm_id);

            $data['uid'] = $uid;
            $data['bm_id'] = $bm_id;
            $data['order_no']=date('ymdHis').mt_rand(100,999).substr($uid,-1);//生成订单号
            $data['title'] = $_POST['title'];
            $data['amount'] = $_POST['amount'];
            $data['create_time'] = NOW;
            $data['pay_time'] = strtotime($_POST['pay_time']);
            $data['pay_type'] = 3;  //3：线下支付
            $data['status'] = 1;    //线下已付
            $data['remark'] = trim($_POST['remark']);
            $data['pay_label'] = $_POST['pay_label'];

            $insert_id=db()->insert('x_order',$data);
            if($insert_id){
                $this->ajaxReturn(array('status'=>'y','info'=>'添加成功'));
            }else{
                $this->ajaxReturn(array('status'=>'n','info'=>'添加失败'));
            }
        }else {
            $bm_id=(int)$_GET['bm_id'];
            $taocan_id = db()->getOne('select taocan_id from x_baoming where id='.$bm_id);
            $tpl=$this->Template();
            $tpl->assign('taocan_id',$taocan_id);
            $tpl->display();
        }


    }

    //线下支付——手动修改
    public function edit(){
        if (IS_AJAX){
            $id=(int)$_GET['id'];
            if($id<=0) $this->ajaxReturn(array('status'=>'n','info'=>'订单id错误'));
            $result=offline_wxpay_notify($id);//线下支付通知
            if($result){
                if($result['return_code']=='SUCCESS') {
                    $data['pay_type']=3;//3:线下支付
                    $data['remark'] = trim($_POST['remark']);//备注
                    db()->update('x_order',$data,'id='.$id);
                    $this->ajaxReturn(array('status'=>'y','info'=>'支付成功'));
                }
                else $this->ajaxReturn(array('status'=>'n','info'=>$result['return_msg']));
            }
            else $this->ajaxReturn(array('status'=>'n','info'=>'系统错误，请稍后再试'));
        }else {
            $id=(int)$_GET['id'];
            $res=db()->getRow('select * from x_order where id='.$id);
            $tpl=$this->Template();
            $tpl->assign('vo',$res);
            $tpl->display();
        }


    }

}
